﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data.SqlClient;
using System.Data;
public partial class Admin_News_Info_Add : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {   //检查用户权限
        if (Session["Uid"] == null)
        {
            Response.Redirect("AD_Login.aspx");
            return;
        }
        //情况消息框
        lblMessage.Text = "";
        if (!IsPostBack)
        {
            addOrUpdate();
            
            bind_drp();
            bingThing();
        }
    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        if (lblTitle.Text.IndexOf("添加") >= 0)
        {
            add();

        }
        else
        {
            update();
        }
    }
    /// <summary>
    /// 填充下拉列表内容
    /// <remark>2010-10-23 河北大学信管团队-水亚亚 </remark>
    /// </summary>
    private void bind_drp()
    {
        string sql = "select newsType from News_Type";
        drpNewsType.DataSource = DataAccess.GetDataTable(sql, null);

        drpNewsType.DataBind();

    }
    /// <summary>
    /// 如果是修改新闻，则填充原始数据
    /// <remark>2010-10-23 河北大学信管团队-水亚亚 </remark>
    /// </summary>
    private void bingThing()
    {
        if (!addOrUpdate())
        {
            string cmd = "select * from News_Info where newsId=@newsId";
            SqlParameter[] paras = new SqlParameter[] { 
                new SqlParameter("@newsId",Request.QueryString["ID"])
            };
            DataTable dt = DataAccess.GetDataTable(cmd, paras);
            DataRow dr = dt.Rows[0];
            chkShow.Checked = (Boolean)dr["newsIsShow"];
            drpNewsType.SelectedValue = dr["newsType"].ToString();
            chkHot.Checked = (Boolean)dr["newsHot"];
            txtContent.Text = dr["newsContent"].ToString();
            txtFrom.Text = dr["newsFrom"].ToString();
            txtKeyWords.Text = dr["newsKeywords"].ToString();
            chkTop.Checked = (Boolean)dr["newsTop"];
            txtTitle.Text = (String)dr["newsTitle"];
        }
    }
    /// <summary>
    /// 添加新闻
    /// <remark>2010-10-23 河北大学信管团队-水亚亚 </remark>
    /// </summary>
    private void add()
    {

        
        string sql = "insert into News_Info (newsIsShow,newsType,newsFrom,newsTitle,newsContent,newsKeywords,newsHits,newsTime,newsHot,newsTop)values(@newsIsShow,@newsType,@newsFrom,@newsTitle,@newsContent,@newsKeywords,@newsHits,@newsTime,@newsHot,@newsTop)";

        SqlParameter[] paras = { 
           new SqlParameter("@newsIsShow",SqlDbType.Bit),
            new SqlParameter("@newsType",SqlDbType.NVarChar,20),
            new SqlParameter("@newsFrom",SqlDbType.NVarChar,20),
            new SqlParameter("@newsTitle",SqlDbType.NVarChar,50),
            new SqlParameter("@newsContent",SqlDbType.NText),
            new SqlParameter("@newsKeywords",SqlDbType.NVarChar,50),
            new SqlParameter("@newsHits",SqlDbType.Int),
            new SqlParameter("@newsTime",SqlDbType.DateTime),
            new SqlParameter("@newsHot",SqlDbType.Bit),
            new SqlParameter("@newsTop",SqlDbType.Bit)
                               };
        paras[0].Value = chkShow.Checked == true ? 1 : 0;
        paras[1].Value = drpNewsType.SelectedItem.Text;
        paras[2].Value = txtFrom.Text.Trim();
        paras[3].Value = txtTitle.Text.Trim();
        paras[4].Value = txtContent.Text.Trim();
        paras[5].Value = txtKeyWords.Text.Trim();
        paras[6].Value = 0;
        paras[7].Value = System.DateTime.Now;
        paras[8].Value = chkHot.Checked == true ? 1 : 0;
        paras[9].Value = chkTop.Checked == true ? 1 : 0;
        if (DataAccess.ExecuteSQL(sql, paras))
        {
            lblMessage.Text = "添加成功";
            Response.Write("<script>alert('添加新闻成功！');location.href='News_Info_Manage.aspx';</script>");
        }
        else
            lblMessage.Text = "添加失败";
    }
    /// <summary>
    /// 更新新闻
    ///       <remark>2010-10-23 河北大学信管团队-水亚亚 </remark>
    /// </summary>
    private void update()
    {

        string id = Request.QueryString["ID"];
        string sql = "update News_Info set newsIsShow=@newsIsShow,newsType=@newsType,newsFrom=@newsFrom,newsTitle=@newsTitle,newsContent=@newsContent,newsKeyWords=@newsKeywords,newsHot=@newsHot,newsTop=@newsTop where newsId='" + id + "'";
        SqlParameter[] paras = { 
           new SqlParameter("@newsIsShow",SqlDbType.Bit),
            new SqlParameter("@newsType",SqlDbType.NVarChar,20),
            new SqlParameter("@newsFrom",SqlDbType.NVarChar,20),
            new SqlParameter("@newsTitle",SqlDbType.NVarChar,50),
            new SqlParameter("@newsContent",SqlDbType.NText),
            new SqlParameter("@newsKeywords",SqlDbType.NVarChar,50),
            new SqlParameter("@newsHot",SqlDbType.Bit),
            new SqlParameter("@newsTop",SqlDbType.Bit)
                               };
        paras[0].Value = chkShow.Checked == true ? 1 : 0;
        paras[1].Value = drpNewsType.SelectedItem.Text;
        paras[2].Value = txtFrom.Text.Trim();
        paras[3].Value = txtTitle.Text.Trim();
        paras[4].Value = txtContent.Text.Trim();
        paras[5].Value = txtKeyWords.Text.Trim();
        paras[6].Value = chkHot.Checked == true ? 1 : 0;
        paras[7].Value = chkTop.Checked == true ? 1 : 0;
        if (DataAccess.ExecuteSQL(sql, paras))
        {
            Response.Write("<script>alert('修改新闻成功！');location.href='News_Info_Manage.aspx';</script>");
        }
        else
            lblMessage.Text = "修改失败";

    }
    /// <summary>
    /// 判断是修改还是新建
    /// <remark>2010-10-23 河北大学信管团队-水亚亚 </remark>
    /// </summary>
    /// <returns>判断结果：true:新建，false:修改</returns>
    private bool addOrUpdate()
    {
        ///不为空，则返回false  ,更新内容
        if (Request.QueryString["ID"] != null && Request.QueryString["ID"] != "")
        {
            lblTitle.Text = "修改新闻";
            return false;
        }
        lblTitle.Text = "添加新闻";
        return true;
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {

        Response.Redirect("News_Info_Manage.aspx");
    }
  
}